package com.spzx.order.receiver;

import com.rabbitmq.client.Channel;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.order.configure.DeadLetterMqConfig;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class TestReceiver {
    @Autowired
    private RedisTemplate redisTemplate;
    @SneakyThrows //等价于   /*throws Exception*/
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_TEST, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_TEST, durable = "true"),
            key = MqConst.ROUTING_TEST)
    )
    public void test(String content, Message message, Channel channel) {
        //都可以
        log.info("接收消息：{}", content);
        //log.info("接收消息：{}", new String(message.getBody()));
    }
    /**
     * 监听确认消息
     * @param message
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_TEST,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_CONFIRM,durable = "true"),
            key = MqConst.ROUTING_CONFIRM
    ))
    public void confirm(String content,Message message,Channel channel){
        log.info("接收确认消息{}",content);
        // false 确认一个消息，true 批量确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
    /**
     * 监听延迟消息,监听死信队列。
     * @param msg
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(queues = {DeadLetterMqConfig.queue_dead_2})
    public void getDeadLetterMsg(String msg,Message message,Channel channel){
        log.info("私信消费者：{}",msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
    //监听延迟消息
    @SneakyThrows
    @RabbitListener(queues = {DeadLetterMqConfig.queue_dead_2})
    public void getDeadLetterMsg1(String msg,Message message,Channel channel){
        //接收消息，消费者端判断是否需要做幂等性处理，如果业务保证幂等性，基于redis setnx保证
        String Key="mq"+msg;
        Boolean flag = redisTemplate.opsForValue().setIfAbsent(Key, "", 200, TimeUnit.SECONDS);
        if (!flag) {
            //说明该业务数据已经被执行
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        //执行相关业务

        //确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
